﻿using furion.blog.Utils;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Web.Core.Handlers
{
    public class ExceptionHandler : IAsyncExceptionFilter
    {
        public Task OnExceptionAsync(ExceptionContext context)
        {
            //进行异常处理
            // 如果异常没有被处理则进行处理
            if (context.ExceptionHandled == false)
            {
                switch (context.Exception)
                {
                    case Exception:
                        {
                            context.Result = new ObjectResult(ResultUtil.Failure(context.Exception.Message));
                        }; break;
                    default:; break;
                }
            }
            // 设置为true，表示异常已经被处理了
            context.ExceptionHandled = true;
            return Task.CompletedTask;
        }
    }
}